$('.overflow').niceScroll();
var localIndex;
var localData;
var localCompanyId;
var localBillDate;
function beginAudit() {
	if($('.tag-select-limited').val() == null) {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
    			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
    			"需选择公司名称"+
				"</div>");
		return;
	}
	var company_id = $('.tag-select-limited').val()[0];
	var bill_date = $('#bill_date').val();
	if(bill_date == undefined || bill_date == '') {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
                			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                			"需填写会计期间"+
            				"</div>");
		return;
	}
	localCompanyId = company_id;
	localBillDate = bill_date;
	getData(company_id, bill_date);
}

function getData(company_id, bill_date) {
	$.post('/ynrfinance/voucherAudit', {bill_date:bill_date, company_id:company_id}, function(data) {
		if(data.error_code == 1) {
			$('#dataDiv input').each(function(){$(this).val('');});
			$('#voucherTable tbody').html('');
			$('#invoice .row .deleteMark').nextAll().remove();
			$('#warning').html('');
			var data = JSON.parse(data.dataList);
			localData = data;
			for(var index in data) {
				localIndex = index;
				var item = data[index];
				if(item.correct == 1) continue;
				console.log(item.type);
				fillData(item);
				return;
			}
			$('#warning').html('<p>当前月份未查到需要审核的凭证！</p>');
		} else {
			$('#warning').html('<p>没有查询到数据，请更换日期后重试！</p>');
		}
	});
}

function next() {
	var previousVoucherList = localData[localIndex].voucherList;
	var ids = [];
	for(var index in previousVoucherList) {
		ids.push(previousVoucherList[index].id);
	}
	$.post('/ynrfinance/updateVoucherStatus', {ids : JSON.stringify(ids)}, function(data) {
		console.log('更新状态位结果：' + data.error_code);
	});
	$('#expense').css('display', 'none');
	$('#invoice').css('display', 'none');
	$('#salary').css('display', 'none');
	$('#bank').css('display', 'none');
	$('#dataDiv input').each(function(){$(this).val('');});
	$('#voucherTable tbody').html('');
	$('#invoice .row .deleteMark').nextAll().remove();
	if(localIndex++ >= (localData.length-1)) {
		$('#voucherTable').css('display', 'none');
		$('#voucherTable').nextAll().remove();
		$('#warning').html('<p>凭证已审核结束，即将展现所有凭证！</p>');
		$('#warning').css('display', '');
		setTimeout(function() {
			$.post('/ynrfinance/getAllCorrectVoucher', {company_id : localCompanyId, bill_date : localBillDate}, function(data) {
				var voucherList = data.data;
				for(var index in voucherList) {
					$('#voucherTable tbody').append('<tr>'
							+ '<td>'+voucherList[index].bill_date+'</td>'
							+ '<td>'+(voucherList[index].voucher_type==1?'转':'银')+'</td>'
							+ '<td>'+voucherList[index].voucher_num+'</td>'
							+ '<td>'+voucherList[index].annex_mount+'</td>'
							+ '<td>'+voucherList[index].abstract_str+'</td>'
							+ '<td>'+voucherList[index].subject+'</td>'
							+ '<td>'+voucherList[index].debit+'</td>'
							+ '<td>'+voucherList[index].credit+'</td>'
							+ '<td>'+voucherList[index].originator+'</td>'
							+ '</tr>');
				}
				$('#warning').css('display', 'none');
				$('#voucherTable').css('display', '');
			});
		}, 5000);
		return;
	}
	var isCorrect = true;
	while(isCorrect) {
		if(localData[localIndex].correct == 0) {
			isCorrect = false;
		}
	}
	var item = localData[localIndex];
	fillData(item);
}
function fillData(item) {
	if(item.type == 'invoice') {
		$('#xfMc').val(item.xfMc);
		$('#gfMc').val(item.gfMc);
		$('#fphm').val(item.fphm);
		$('#fpdm').val(item.fpdm);
		$('#kprq').val(item.kprq);
		$('#fplx').val(item.fplxName);
		$('#jshj').val(item.sumamount);
		$('#tax').val(item.taxamount);
		var itemList = item.itemList;
		for(var index in itemList) {
			$('#invoice .row').append('<div class="form-group" style="text-align:center">'
					+ '<label class="col-md-1 control-label" style="margin-top:4px">摘要</label>'
                    + '<div class="col-md-2"><input class="form-control input-sm" value="'+itemList[index].name+'"></div>'
                    + '<label class="col-md-1 control-label" style="margin-top:4px">金额</label>'
                    + '<div class="col-md-2"><input class="form-control input-sm" value="'+itemList[index].priceSum+'"></div>'
                    + '<label class="col-md-1 control-label" style="margin-top:4px">税额</label>'
                    + '<div class="col-md-2"><input class="form-control input-sm" value="'+itemList[index].taxSum+'"></div>'
                    + '<label class="col-md-1 control-label" style="margin-top:4px">税率</label>'
                    + '<div class="col-md-2"><input class="form-control input-sm" value="'+itemList[index].taxRate+'"></div>');
		}
		$('#invoice .row').append('<hr class="whiter" style="margin:10px"/>');
		var voucherList = item.voucherList;
		for(var index in voucherList) {
			$('#voucherTable tbody').append('<tr>'
					+ '<td>'+voucherList[index].bill_date+'</td>'
					+ '<td>转</td>'
					+ '<td>'+voucherList[index].voucher_num+'</td>'
					+ '<td>'+voucherList[index].annex_mount+'</td>'
					+ '<td>'+voucherList[index].abstract_str+'</td>'
					+ '<td>'+voucherList[index].subject+'</td>'
					+ '<td>'+voucherList[index].debit+'</td>'
					+ '<td>'+voucherList[index].credit+'</td>'
					+ '<td>'+voucherList[index].originator+'</td>'
					+ '</tr>');
		}
		$('#invoice').css('display', '');
		$('#voucher').css('display', '');
	} else if(item.type == 'expense') {
		$('#amount').val(item.amount);
		$('#expense_type').val(item.expense_type);
		$('#department').val(item.department);
		$('#tax').val(item.tax);
		$('#invoice .row').append('<hr class="whiter" style="margin:10px"/>');
		var voucherList = item.voucherList;
		for(var index in voucherList) {
			$('#voucherTable tbody').append('<tr>'
					+ '<td>'+voucherList[index].bill_date+'</td>'
					+ '<td>转</td>'
					+ '<td>'+voucherList[index].voucher_num+'</td>'
					+ '<td>'+voucherList[index].annex_mount+'</td>'
					+ '<td>'+voucherList[index].abstract_str+'</td>'
					+ '<td>'+voucherList[index].subject+'</td>'
					+ '<td>'+voucherList[index].debit+'</td>'
					+ '<td>'+voucherList[index].credit+'</td>'
					+ '<td>'+voucherList[index].originator+'</td>'
					+ '</tr>');
		}
		$('#expense').css('display', '');
		$('#voucher').css('display', '');
	} else if(item.type == 'salary') {
		$('#employee_nature').val(item.employee_nature);
		$('#phone').val(item.phone);
		$('#name').val(item.name);
		$('#wage').val(item.wage);
		$('#endowment_person').val(item.endowment_person);
		$('#unemployment_person').val(item.unemployment_person);
		$('#medical_person').val(item.medical_person);
		$('#house_provident_person').val(item.house_provident_person);
		$('#endowment_company').val(item.endowment_company);
		$('#unemployment_company').val(item.unemployment_company);
		$('#medical_company').val(item.medical_company);
		$('#injury_company').val(item.injury_company);
		$('#birth_company').val(item.birth_company);
		$('#house_provident_company').val(item.house_provident_company);
		$('#tax').val(item.tax);
		$('#real_wage').val(item.real_wage);
		$('#invoice .row').append('<hr class="whiter" style="margin:10px"/>');
		var voucherList = item.voucherList;
		for(var index in voucherList) {
			$('#voucherTable tbody').append('<tr>'
					+ '<td>'+voucherList[index].bill_date+'</td>'
					+ '<td>转</td>'
					+ '<td>'+voucherList[index].voucher_num+'</td>'
					+ '<td>'+voucherList[index].annex_mount+'</td>'
					+ '<td>'+voucherList[index].abstract_str+'</td>'
					+ '<td>'+voucherList[index].subject+'</td>'
					+ '<td>'+voucherList[index].debit+'</td>'
					+ '<td>'+voucherList[index].credit+'</td>'
					+ '<td>'+voucherList[index].originator+'</td>'
					+ '</tr>');
		}
		$('#salary').css('display', '');
		$('#voucher').css('display', '');
	} else if(item.type == 'bank') {
		$('#trade_time').val(item.trade_time);
		$('#income').val(item.income);
		$('#cost').val(item.cost);
		$('#balance').val(item.balance);
		$('#pay_account_name').val(item.pay_account_name);
		$('#abstract_').val(item.abstract_);
		$('#purpose').val(item.purpose);
		$('#remark').val(item.remark);
		var voucherList = item.voucherList;
		for(var index in voucherList) {
			$('#voucherTable tbody').append('<tr>'
					+ '<td>'+voucherList[index].bill_date+'</td>'
					+ '<td>转</td>'
					+ '<td>'+voucherList[index].voucher_num+'</td>'
					+ '<td>'+voucherList[index].annex_mount+'</td>'
					+ '<td>'+voucherList[index].abstract_str+'</td>'
					+ '<td>'+voucherList[index].subject+'</td>'
					+ '<td>'+voucherList[index].debit+'</td>'
					+ '<td>'+voucherList[index].credit+'</td>'
					+ '<td>'+voucherList[index].originator+'</td>'
					+ '</tr>');
		}
		$('#bank').css('display', '');
		$('#voucher').css('display', '');
	}
}
function faDepreciation() {
	$('.table tbody').html('');
	if($('.tag-select-limited').val() == null) {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
    			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
    			"需选择公司名称"+
				"</div>");
		return;
	}
	var company_id = $('.tag-select-limited').val()[0];
	var bill_date = $('#bill_date').val();
	if(bill_date == undefined || bill_date == '') {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
                			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                			"需填写会计期间"+
            				"</div>");
		return;
	}
	
	$.post('/ynrfinance/getFaDepreciation', {bill_date:bill_date, company_id:company_id}, function(data) {
		var faList = data.data; 
		if(faList != null && faList.length != 0) {
			for(var index in faList) {
				$('.table tbody').append('<tr>'
						+ '<td>'+faList[index].order+'</td>'
						+ '<td>'+faList[index].date+'</td>'
						+ '<td>'+faList[index].classification+'</td>'
						+ '<td>'+faList[index].name+'</td>'
						+ '<td>'+faList[index].original_value+'</td>'
						+ '<td>'+faList[index].scrap_value+'</td>'
						+ '<td>'+faList[index].months+'</td>'
						+ '<td>'+faList[index].rm_value_rate+'</td>'
						+ '<td>'+faList[index].monthly_depreciation+'</td>'
						+ '<td>'+faList[index].accumulated_amortization_month+'</td>'
						+ '<td>'+faList[index].depreciation_this_month+'</td>'
						+ '<td>'+faList[index].accumulated_depreciation+'</td>'
						+ '<td>'+faList[index].nav+'</td>'
						+ '</tr>');
			}
		} 
		$('#modalWider').modal('show');
	});
}
function updateDict() {
	$('.table tbody').html('');
	if($('.tag-select-limited').val() == null) {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
    			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
    			"需选择公司名称"+
				"</div>");
		return;
	}
	var company_id = $('.tag-select-limited').val()[0];
	var bill_date = $('#bill_date').val();
	if(bill_date == undefined || bill_date == '') {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
                			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                			"需填写会计期间"+
            				"</div>");
		return;
	}
	var dict = $('#dict').val();
	var subject = $('#subject').val()
	var data = localData[localIndex];
	if(dict == undefined || dict == '') {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
                			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                			"需填写字典"+
            				"</div>");
		return;
	}
	if(subject == undefined || subject == '') {
		$('#error_tips').html("<div class='alert alert-danger alert-dismissable fade in'>" +
                			"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button>" +
                			"需填写科目代码"+
            				"</div>");
		return;
	}
	$.post('/ynrfinance/updateDict', {billDate:bill_date, companyId:company_id, subject : subject, dict : dict, data : JSON.stringify(data)}, function(data) {
		getData(company_id, bill_date);
	});
}